UNPKG

4.75 kBHTMLView Raw
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <title>JSDoc: Tutorial: Autoupdating Collections</title>
6
7 <script src="scripts/prettify/prettify.js"> </script>
8 <script src="scripts/prettify/lang-css.js"> </script>
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13 <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14</head>
15
16<body>
17
18<div id="main">
19
20 <h1 class="page-title">Tutorial: Autoupdating Collections</h1>
21
22 <section>
23
24<header>
25
26
27 <h2>Autoupdating Collections</h2>
28</header>
29
30<article>
31 <h1>The Autoupdate Feature for Collections</h1><p>Autoupdate can be enabled on a per-collection basis via the constructor option <code>autoupdate: true</code>. The feature requires <code>Object.observe</code> (currently implemented in Chrome 36+, io.js and Node.js 0.12+). If observers are not available, the option will be ignored.</p>
32<p>Autoupdate automatically calls <code>update(doc)</code> whenever a document is modified, which is necessary for index updates and dirty-marks (used to determine whether the DB has been modified and should be persisted).</p>
33<p>Enabling this feature basically means, that all manual <code>update</code> calls can be omitted.</p>
34<h2>Example</h2><pre class="prettyprint source lang-js"><code>var doc = collection.by(&quot;name&quot;, &quot;John&quot;);
35
36doc.name = &quot;Peter&quot;;
37doc.age = 32;
38doc.gender = &quot;male&quot;;
39
40collection.update(doc); // This line can be safely removed.</code></pre><p>Autoupdate will call <code>update</code> at the end of the current event loop cycle and thus only calls <code>update</code> once, even when multiple changes were made.</p>
41<h2>Error handling</h2><p>There is one important difference between autoupdate and manual updates. If for example a document change violates a unique key constraint, <code>update</code> will synchronously throw an error which can be catched synchronously:</p>
42<pre class="prettyprint source lang-js"><code>var collection = db.addCollection(&quot;test&quot;, [
43 unique: [&quot;name&quot;]
44]);
45
46collection.insert({ name: &quot;Peter&quot; });
47
48var doc = collection.insert({ name: &quot;Jack&quot; });
49doc.name = &quot;Peter&quot;;
50
51try {
52 collection.update(doc);
53} catch(err) {
54 doc.name = &quot;Jack&quot;;
55}</code></pre><p>Since autoupdate calls <code>update</code> asynchronously, you cannot catch errors via <code>try-catch</code>. Instead you have to use event listeners:</p>
56<pre class="prettyprint source lang-js"><code>var collection = db.addCollection(&quot;test&quot;, [
57 unique: [&quot;name&quot;],
58 autoupdate: true
59]);
60
61collection.insert({ name: &quot;Peter&quot; });
62
63var doc = collection.insert({ name: &quot;Jack&quot; });
64doc.name = &quot;Peter&quot;;
65
66collection.on(&quot;error&quot;, function(errDoc) {
67 if(errDoc === doc) {
68 doc.name = &quot;Jack&quot;;
69 }
70});</code></pre><p>This can become quite tedious, so you should consider performing checks before updating documents instead.</p>
71</article>
72
73</section>
74
75</div>
76
77<nav>
78 <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Collection.html">Collection</a></li><li><a href="DynamicView.html">DynamicView</a></li><li><a href="Loki.html">Loki</a></li><li><a href="LokiEventEmitter.html">LokiEventEmitter</a></li><li><a href="LokiFsAdapter.html">LokiFsAdapter</a></li><li><a href="LokiFsStructuredAdapter.html">LokiFsStructuredAdapter</a></li><li><a href="LokiIndexedAdapter.html">LokiIndexedAdapter</a></li><li><a href="LokiLocalStorageAdapter.html">LokiLocalStorageAdapter</a></li><li><a href="LokiMemoryAdapter.html">LokiMemoryAdapter</a></li><li><a href="LokiPartitioningAdapter.html">LokiPartitioningAdapter</a></li><li><a href="Resultset.html">Resultset</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-Autoupdating Collections.html">Autoupdating Collections</a></li><li><a href="tutorial-Changes API.html">Changes API</a></li><li><a href="tutorial-Collection Transforms.html">Collection Transforms</a></li><li><a href="tutorial-Indexing and Query performance.html">Indexing and Query performance</a></li><li><a href="tutorial-Loki Angular.html">Loki Angular</a></li><li><a href="tutorial-Persistence Adapters.html">Persistence Adapters</a></li><li><a href="tutorial-Query Examples.html">Query Examples</a></li></ul>
79</nav>
80
81<br class="clear">
82
83<footer>
84 Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Sun Dec 18 2016 19:39:52 GMT-0500 (Eastern Standard Time)
85</footer>
86
87<script> prettyPrint(); </script>
88<script src="scripts/linenumber.js"> </script>
89</body>
90</html>
\No newline at end of file